const logger = require('log4js').getLogger()

exports.push = obj => {
    return new Promise((resolve, reject) => {
        if (!obj.content) {
            reject({})
            return
        }
        require('../util/getHtml')({
            url: 'https://api.alertover.com/v1/alert',
            data: [
                "source=s-199877af-366c-44d4-a769-469fbc0f",
                "receiver=g-5fb32322-cbde-418c-8571-71f85191",
                "content=" + obj.content,
                "title=" + (obj.title || 'system')
            ].join('&')
        }).then(result => {
            //logger.debug('[' + __filename + ':' + __line + ']', result)
			try {
				var obj = JSON.parse(result)
				if (obj.code == 0) {
					resolve(obj)
				} else {
					reject(obj)
				}
			} catch(err) {
				logger.error('[' + __filename + ':' + __line + ']', result.replace(/\n/g, ''))
				reject()
			}
        }, result => {
            reject(result)
        }).catch(err => {
            reject(err)
        })
    })
}